﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Uber.Collections;
using Uber.HabboHotel.Pets;
using Uber.HabboHotel.Navigators;
using Uber.HabboHotel.Items;
using Uber.HabboHotel.GameClients;
using Uber.HabboHotel.Users.Badges;
using Uber.HabboHotel.Misc;
using Uber.HabboHotel.Pathfinding;
using Uber.HabboHotel.Advertisements;
using Uber.HabboHotel.Users.Messenger;
using Uber.HabboHotel.Rooms;
using Uber.HabboHotel.Catalogs;
using Uber.Storage;
using Uber.HabboHotel.RoomBots;

namespace Uber.Messages
{
    partial class GameClientMessageHandler
    {
        private void GetAdvertisement()
        {
            RoomAdvertisement Ad = UberEnvironment.GetGame().GetAdvertisementManager().GetRandomRoomAdvertisement();

            GetResponse().Init(258);

            if (Ad == null)
            {
                GetResponse().AppendStringWithBreak("");
                GetResponse().AppendStringWithBreak("");
            }
            else
            {
                GetResponse().AppendStringWithBreak(Ad.AdImage);
                GetResponse().AppendStringWithBreak(Ad.AdLink);

                Ad.OnView();
            }

            SendResponse();
        }

        private void GetPub()
        {
            uint Id = Request.PopWiredUInt();

            RoomData Data = UberEnvironment.GetGame().GetRoomManager().GenerateRoomData(Id);

            if (Data == null)
            {
                return;
            }

            if (Id == UberEnvironment.uVipRoomId && !Session.GetHabbo().IsUltraVip)
            {
                Session.SendNotif("Buy Ultra VIP to use this public room!");
                return;
            }

            if (Data.Type == "private")
            {
                PrepareRoomForUser(Id, "");
            }

            GetResponse().Init(453);
            GetResponse().AppendUInt(Data.Id);
            GetResponse().AppendStringWithBreak(Data.CCTs);
            GetResponse().AppendUInt(Data.Id);
            SendResponse();
        }

        private void OpenFlat()
        {
            uint Id = Request.PopWiredUInt();
            string Password = Request.PopFixedString();
            int Junk = Request.PopWiredInt32();

            RoomData Data = UberEnvironment.GetGame().GetRoomManager().GenerateRoomData(Id);

            if (Data == null || Data.Type != "private")
            {
                return;
            }

            PrepareRoomForUser(Id, Password);
        }

        private void OpenPub()
        {
            int Junk = Request.PopWiredInt32();
            uint Id = Request.PopWiredUInt();
            int Junk2 = Request.PopWiredInt32();

            RoomData Data = UberEnvironment.GetGame().GetRoomManager().GenerateRoomData(Id);

            if (Data == null || Data.Type != "public")
            {
                return;
            }

            PrepareRoomForUser(Data.Id, "");
        }

        private void GetGroupBadges()
        {
            GetResponse().Init(309);
            GetResponse().AppendStringWithBreak("");
            SendResponse();
        }

        private void GetInventory()
        {
            Session.SendMessage(Session.GetHabbo().GetInventoryComponent().SerializeItemInventory());
        }

        private void GetRoomData1()
        {
            if (Session.GetHabbo().LoadingRoom <= 0)
            {
                return;
            }

            GetResponse().Init(297);
            GetResponse().AppendBoolean(false);
            SendResponse();
        }

        private void GetRoomData2()
        {
            if (Session.GetHabbo().LoadingRoom <= 0)
            {
                return;
            }

            RoomData Data = UberEnvironment.GetGame().GetRoomManager().GenerateRoomData(Session.GetHabbo().LoadingRoom);

            if (Data == null)
            {
                return;
            }

            if (Data.Model == null)
            {
                Session.SendNotif("Sorry, model data is missing from this room and therefore cannot be loaded.");
                Session.SendMessage(new ServerMessage(18));
                ClearRoomLoading();
                return;
            }

            Session.SendMessage(Data.Model.SerializeHeightmap());
            Session.SendMessage(Data.Model.SerializeRelativeHeightmap());
        }

        private void GetRoomData3()
        {
            if (Session.GetHabbo().LoadingRoom <= 0 || !Session.GetHabbo().LoadingChecksPassed)
            {
                return;
            }

            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().LoadingRoom);

            if (Room == null)
            {
                return;
            }

            ClearRoomLoading();

            GetResponse().Init(30);

            if (Room.Model.StaticFurniMap != "")
            {
                GetResponse().AppendStringWithBreak(Room.Model.StaticFurniMap);
            }
            else
            {
                GetResponse().AppendInt32(0);
            }

            SendResponse();

            if (Room.Type == "private")
            {
                var FloorItems = Room.FloorItems;
                var WallItems = Room.WallItems;

                GetResponse().Init(32);
                GetResponse().AppendInt32(FloorItems.Count);

                for(int i = 0; i < FloorItems.Count; i++)
                {
                    RoomItem Item = FloorItems[i];

                    Item.Serialize(GetResponse());
                }

                SendResponse();

                GetResponse().Init(45);
                GetResponse().AppendInt32(WallItems.Count);

                for (int i = 0; i < WallItems.Count; i++)
                {
                    RoomItem Item = WallItems[i];

                    Item.Serialize(GetResponse());
                }

                SendResponse();
            }

            Room.AddUserToRoom(Session, Session.GetHabbo().SpectatorMode);

            var UsersToDisplay = new List<RoomUser>();

            for (int i = 0; i < Room.UserList.Count; i++)
            {
                RoomUser User = Room.UserList[i];

                if (User == null || User.IsSpectator)
                {
                    continue;
                }

                UsersToDisplay.Add(User);
            }

            GetResponse().Init(28);
            GetResponse().AppendInt32(UsersToDisplay.Count);

            for (int i = 0; i < UsersToDisplay.Count; i++)
            {
                RoomUser User = UsersToDisplay[i];

                User.Serialize(GetResponse());
            }

            SendResponse();

            if (Room.Type == "public")
            {
                GetResponse().Init(471);
                GetResponse().AppendBoolean(false);
                GetResponse().AppendStringWithBreak(Room.ModelName);
                GetResponse().AppendBoolean(false);
                SendResponse();
            }
            else if (Room.Type == "private")
            {
                GetResponse().Init(472);
                GetResponse().AppendBoolean(Room.Hidewall);
                SendResponse();

                GetResponse().Init(471);
                GetResponse().AppendBoolean(true);
                GetResponse().AppendUInt(Room.RoomId);
                GetResponse().AppendBoolean(Room.CheckRights(Session, true));
                SendResponse();

                GetResponse().Init(454);

                RoomData Data = UberEnvironment.GetGame().GetRoomManager().GenerateRoomData(Room.RoomId);

                Data.Serialize(GetResponse(), true);

                SendResponse();
            }

            ServerMessage Updates = Room.SerializeStatusUpdates(true);

            if (Updates != null)
            {
                Session.SendMessage(Updates);
            }

            for (int i = 0; i < Room.UserList.Count; i++)
            {
                RoomUser User = Room.UserList[i];

                if (User == null || User.IsSpectator)
                {
                    continue;
                }

                if (User.IsDancing)
                {
                    GetResponse().Init(480);
                    GetResponse().AppendInt32(User.VirtualId);
                    GetResponse().AppendInt32(User.DanceId);
                    SendResponse();
                }

                if (User.HasItemEffect)
                {
                    GetResponse().Init(485);
                    GetResponse().AppendInt32(User.VirtualId);
                    GetResponse().AppendInt32(User.GetItemEffect);
                    SendResponse();
                }

                if (User.IsAsleep)
                {
                    GetResponse().Init(486);
                    GetResponse().AppendInt32(User.VirtualId);
                    GetResponse().AppendBoolean(true);
                    SendResponse();
                }

                if (User.CarryItemID > 0 && User.CarryTimer > 0)
                {
                    GetResponse().Init(482);
                    GetResponse().AppendInt32(User.VirtualId);
                    GetResponse().AppendInt32(User.CarryTimer);
                    SendResponse();
                }

                if (!User.IsBot)
                {
                    if (User.GetClient().GetHabbo() != null && User.GetClient().GetHabbo().GetAvatarEffectsInventoryComponent() != null && User.GetClient().GetHabbo().GetAvatarEffectsInventoryComponent().CurrentEffect >= 1)
                    {
                        GetResponse().Init(485);
                        GetResponse().AppendInt32(User.VirtualId);
                        GetResponse().AppendInt32(User.GetClient().GetHabbo().GetAvatarEffectsInventoryComponent().CurrentEffect);
                        SendResponse();
                    }
                }
            }
        }

        public void PrepareRoomForUser(uint Id, string Password)
        {
            ClearRoomLoading();

            if (UberEnvironment.GetGame().GetRoomManager().GenerateRoomData(Id) == null)
            {
                Session.SendNotif("RoomData not found! Room is invalid.");
                return;
            }

            if (Session.GetHabbo().InRoom)
            {
                Room OldRoom = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

                if (OldRoom != null)
                {
                    OldRoom.RemoveUserFromRoom(Session, false, false);
                }
            }

            if (!UberEnvironment.GetGame().GetRoomManager().IsRoomLoaded(Id))
            {
                UberEnvironment.GetGame().GetRoomManager().LoadRoom(Id);
            }

            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Id);

            if (Room == null)
            {
                return;
            }

            Session.GetHabbo().LoadingRoom = Id;

            if (Room.UserIsBanned(Session.GetHabbo().Id))
            {
                if (Room.HasBanExpired(Session.GetHabbo().Id))
                {
                    Room.RemoveBan(Session.GetHabbo().Id);
                }
                else
                {
                    GetResponse().Init(224);
                    GetResponse().AppendInt32(4);
                    SendResponse();

                    GetResponse().Init(18);
                    SendResponse();

                    return;
                }
            }

            if (Room.UsersNow >= Room.UsersMax)
            {
                if (!UberEnvironment.GetGame().GetRoleManager().RankHasRight(Session.GetHabbo().Rank, "fuse_enter_full_rooms"))
                {
                    GetResponse().Init(224);
                    GetResponse().AppendBoolean(true);
                    SendResponse();

                    GetResponse().Init(18);
                    SendResponse();

                    return;
                }
            }

            if (Room.Type == "public")
            {
                if (Room.State > 0 && !Session.GetHabbo().HasFuse("fuse_mod"))
                {
                    Session.SendNotif("This public room is accessible to Uber staff only.");

                    GetResponse().Init(18);
                    SendResponse();

                    return;
                }

                GetResponse().Init(166);
                GetResponse().AppendStringWithBreak("/client/public/" + Room.ModelName + "/0");
                SendResponse();
            }
            else if (Room.Type == "private")
            {
                GetResponse().Init(19);
                SendResponse();

                if (!Session.GetHabbo().HasFuse("fuse_enter_any_room") && !Room.CheckRights(Session, true) && !Session.GetHabbo().IsTeleporting)
                {
                    if (Room.State == 1)
                    {
                        if (Room.UserCount == 0)
                        {
                            GetResponse().Init(131);
                            SendResponse();
                        }
                        else
                        {
                            GetResponse().Init(91);
                            GetResponse().AppendBreak();
                            SendResponse();

                            ServerMessage RingMessage = new ServerMessage(91);
                            RingMessage.AppendStringWithBreak(Session.GetHabbo().Username);
                            Room.SendMessageToUsersWithRights(RingMessage);
                        }

                        return;
                    }
                    else if (Room.State == 2)
                    {
                        if (Password.ToLower() != Room.Password.ToLower())
                        {
                            GetResponse().Init(33);
                            GetResponse().AppendInt32(-100002);
                            SendResponse();

                            GetResponse().Init(18);
                            SendResponse();

                            return;
                        }
                    }
                }

                GetResponse().Init(166);
                GetResponse().AppendStringWithBreak("/client/private/" + Room.RoomId + "/id");
                SendResponse();
            }

            Session.GetHabbo().LoadingChecksPassed = true;

            LoadRoomForUser();
        }

        private void ReqLoadRoomForUser()
        {
            LoadRoomForUser();
        }

        public void LoadRoomForUser()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().LoadingRoom);

            if (Room == null || !Session.GetHabbo().LoadingChecksPassed)
            {
                return;
            }

            GetResponse().Init(309);
            GetResponse().AppendBreak(); // empty badge :D
            SendResponse();

            GetResponse().Init(69);
            GetResponse().AppendStringWithBreak(Room.ModelName);
            GetResponse().AppendUInt(Room.RoomId);
            SendResponse();

            if (Session.GetHabbo().SpectatorMode)
            {
                GetResponse().Init(254);
                SendResponse();
            }

            if (Room.Type == "private")
            {
                if (Room.Wallpaper != "0.0")
                {
                    GetResponse().Init(46);
                    GetResponse().AppendStringWithBreak("wallpaper");
                    GetResponse().AppendStringWithBreak(Room.Wallpaper);
                    SendResponse();
                }

                if (Room.Floor != "0.0")
                {
                    GetResponse().Init(46);
                    GetResponse().AppendStringWithBreak("floor");
                    GetResponse().AppendStringWithBreak(Room.Floor);
                    SendResponse();
                }

                GetResponse().Init(46);
                GetResponse().AppendStringWithBreak("landscape");
                GetResponse().AppendStringWithBreak(Room.Landscape);
                SendResponse();

                if (Room.CheckRights(Session, true))
                {
                    GetResponse().Init(42);
                    SendResponse();

                    GetResponse().Init(47);
                    SendResponse();
                }
                else if (Room.CheckRights(Session))
                {
                    GetResponse().Init(42);
                    SendResponse();
                }

                GetResponse().Init(345);

                if (Session.GetHabbo().RatedRooms.Contains(Room.RoomId) || Room.CheckRights(Session, true))
                {
                    GetResponse().AppendInt32(Room.Score);
                }
                else
                {
                    GetResponse().AppendInt32(-1);
                }

                SendResponse();

                if (Room.HasOngoingEvent)
                {
                    Session.SendMessage(Room.Event.Serialize(Session));
                }
                else
                {
                    GetResponse().Init(370);
                    GetResponse().AppendStringWithBreak("-1");
                    SendResponse();
                }
            }
        }

        public void ClearRoomLoading()
        {
            Session.GetHabbo().LoadingRoom = 0;
            Session.GetHabbo().LoadingChecksPassed = false;
        }

        private void Talk()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Session.GetHabbo().Id);

            if (User == null)
            {
                return;
            }

            User.Chat(Session, UberEnvironment.FilterInjectionChars(Request.PopFixedString()), false);
        }

        private void Shout()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Session.GetHabbo().Id);

            if (User == null)
            {
                return;
            }

            User.Chat(Session, UberEnvironment.FilterInjectionChars(Request.PopFixedString()), true);
        }

        private void Whisper()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            if (Session.GetHabbo().Muted)
            {
                Session.SendNotif("You are muted.");
                return;
            }

            string Params = UberEnvironment.FilterInjectionChars(Request.PopFixedString());
            string ToUser = Params.Split(' ')[0];
            string Message = Params.Substring(ToUser.Length + 1);

            if (Message.Length <= 0) // useless message :D
            {
                return;
            }

            if (ToUser.ToLower() == Session.GetHabbo().Username.ToLower())
            {
                Session.SendNotif("You can't whisper yourself.");
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Session.GetHabbo().Id);
            RoomUser User2 = Room.GetRoomUserByHabbo(ToUser);

            ServerMessage TellMsg = new ServerMessage();
            TellMsg.Init(25);
            TellMsg.AppendInt32(User.VirtualId);
            TellMsg.AppendStringWithBreak(Message);
            TellMsg.AppendBoolean(false);

            if (User != null && !User.IsBot)
            {
                User.GetClient().SendMessage(TellMsg);
            }

            User.Unidle();

            if (User2 != null && !User2.IsBot)
            {
                if (!User2.GetClient().GetHabbo().MutedUsers.Contains(Session.GetHabbo().Id))
                {
                    User2.GetClient().SendMessage(TellMsg);
                }

                UberEnvironment.GetGame().GetChatlogsManager().AddChatlog(Session.GetHabbo().Id, Room.RoomId, DateTime.Now.Hour, DateTime.Now.Minute, UberEnvironment.GetUnixTimestamp(), "<Whisper to " + User2.GetClient().GetHabbo().Username + ">: " + Message, Session.GetHabbo().Username, DateTime.Now.ToLongDateString());
            }
        }

        private void Move()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Session.GetHabbo().Id);

            if (User == null || !User.CanWalk)
            {
                return;
            }

            int MoveX = Request.PopWiredInt32();
            int MoveY = Request.PopWiredInt32();

            if (MoveX == User.X && MoveY == User.Y)
            {
                return;
            }

            User.MoveTo(MoveX, MoveY);
        }

        private void CanCreateRoom()
        {
            int Limit = 0;
            int Amount = 0;

            if (Session.GetHabbo().GetSubscriptionManager().HasSubscription("habbo_vip") | Session.GetHabbo().Rank > 2)
            {
                Limit = UberEnvironment.GetGame().GetGameConfiguration().CreateRooms_maxRooms_Vip;
            }
            else
            {
                Limit = UberEnvironment.GetGame().GetGameConfiguration().CreateRooms_maxRooms;
            }

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                Amount = dbClient.ReadInt32("SELECT COUNT(*) FROM rooms WHERE owner = '" + Session.GetHabbo().Username + "'");
            }

            GetResponse().Init(512);

            if (Amount >= Limit)
            {
                GetResponse().AppendBoolean(true);
            }
            else
            {
                GetResponse().AppendBoolean(false);
            }

            GetResponse().AppendInt32(Limit);
            SendResponse();
        }

        private void CreateRoom()
        {
            string RoomName = UberEnvironment.FilterInjectionChars(Request.PopFixedString());
            string ModelName = Request.PopFixedString();

            RoomData NewRoom = UberEnvironment.GetGame().GetRoomManager().CreateRoom(Session, RoomName, ModelName);

            if (NewRoom != null)
            {
                GetResponse().Init(59);
                GetResponse().AppendUInt(NewRoom.Id);
                GetResponse().AppendStringWithBreak(NewRoom.Name);
                SendResponse();
            }
        }

        private void GetRoomEditData()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return;
            }

            GetResponse().Init(465);
            GetResponse().AppendUInt(Room.RoomId);
            GetResponse().AppendStringWithBreak(Room.Name);
            GetResponse().AppendStringWithBreak(Room.Description);
            GetResponse().AppendInt32(Room.State);
            GetResponse().AppendInt32(Room.Category);
            GetResponse().AppendInt32(Room.UsersMax);
            GetResponse().AppendInt32((Room.Model.MapSizeX >= 20 || Room.Model.MapSizeY >= 20) ? 50 : 25);
            GetResponse().AppendInt32(Room.TagCount);

            foreach (string Tag in Room.Tags)
            {
                GetResponse().AppendStringWithBreak(Tag);
            }

            GetResponse().AppendInt32(Room.UsersWithRights.Count); // users /w rights count

            foreach (uint UserId in Room.UsersWithRights)
            {
                GetResponse().AppendUInt(UserId);
                GetResponse().AppendStringWithBreak(UberEnvironment.GetGame().GetClientManager().GetNameById(UserId));
            }

            GetResponse().AppendInt32(Room.UsersWithRights.Count); // users /w rights count

            GetResponse().AppendBoolean(Room.AllowPets); // allows pets in room - pet system lacking, so always off
            GetResponse().AppendBoolean(Room.AllowPetsEating); // allows pets to eat your food - pet system lacking, so always off
            GetResponse().AppendBoolean(Room.AllowWalkthrough);
            GetResponse().AppendBoolean(Room.Hidewall);
            SendResponse();
        }

        private void SaveRoomIcon()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return;
            }

            int Junk = Request.PopWiredInt32(); // always 3

            var Items = new SafeDictionary<int, int>();

            int Background = Request.PopWiredInt32();
            int TopLayer = Request.PopWiredInt32();
            int AmountOfItems = Request.PopWiredInt32();

            for (int i = 0; i < AmountOfItems; i++)
            {
                int Pos = Request.PopWiredInt32();
                int Item = Request.PopWiredInt32();

                if (Pos < 0 || Pos > 10)
                {
                    return;
                }

                if (Item < 1 || Item > 27)
                {
                    return;
                }

                if (Items.ContainsKey(Pos))
                {
                    return;
                }

                Items.Add(Pos, Item);
            }

            if (Background < 1 || Background > 24)
            {
                return;
            }

            if (TopLayer < 0 || TopLayer > 11)
            {
                return;
            }

            StringBuilder FormattedItems = new StringBuilder();
            int j = 0;

            foreach (KeyValuePair<int, int> Item in Items)
            {
                if (j > 0)
                {
                    FormattedItems.Append("|");
                }

                FormattedItems.Append(Item.Key + "," + Item.Value);

                j++;
            }

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("UPDATE rooms SET icon_bg = '" + Background + "', icon_fg = '" + TopLayer + "', icon_items = '" + FormattedItems.ToString() + "' WHERE id = '" + Room.RoomId + "' LIMIT 1");
            }

            Room.Icon = new RoomIcon(Background, TopLayer, Items);

            GetResponse().Init(457);
            GetResponse().AppendUInt(Room.RoomId);
            GetResponse().AppendBoolean(true);
            SendResponse();

            GetResponse().Init(456);
            GetResponse().AppendUInt(Room.RoomId);
            SendResponse();

            RoomData Data = new RoomData();
            Data.Fill(Room);

            GetResponse().Init(454);
            GetResponse().AppendBoolean(false);
            Data.Serialize(GetResponse(), false);
            SendResponse();
        }

        private void SaveRoomData()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return;
            }

            int Id = Request.PopWiredInt32();
            string Name = UberEnvironment.FilterInjectionChars(Request.PopFixedString());
            string Description = UberEnvironment.FilterInjectionChars(Request.PopFixedString());
            int State = Request.PopWiredInt32();
            string Password = UberEnvironment.FilterInjectionChars(Request.PopFixedString());
            int MaxUsers = Request.PopWiredInt32();
            int CategoryId = Request.PopWiredInt32();
            int TagCount = Request.PopWiredInt32();

            var Tags = new SafeList<string>();
            StringBuilder formattedTags = new StringBuilder();

            for (int i = 0; i < TagCount; i++)
            {
                if (i > 0)
                {
                    formattedTags.Append(",");
                }

                string tag = UberEnvironment.FilterInjectionChars(Request.PopFixedString().ToLower());

                Tags.Add(tag);
                formattedTags.Append(tag);
            }

            int AllowPets = 0;
            int AllowPetsEat = 0;
            int AllowWalkthrough = 0;
            int Hidewall = 0;

            string _AllowPets = Request.PlainReadBytes(1)[0].ToString();
            Request.AdvancePointer(1);

            string _AllowPetsEat = Request.PlainReadBytes(1)[0].ToString();
            Request.AdvancePointer(1);

            string _AllowWalkthrough = Request.PlainReadBytes(1)[0].ToString();
            Request.AdvancePointer(1);

            string _Hidewall = Request.PlainReadBytes(1)[0].ToString();
            Request.AdvancePointer(1);

            if (Name.Length < 3)
            {
                Session.SendNotif("You have chosen an to-short name for your room! Min 3 chars.");
                return;
            }

            if (State < 0 || State > 2)
            {
                return;
            }

            if (MaxUsers != 10 && MaxUsers != 15 && MaxUsers != 20 && MaxUsers != 25 && MaxUsers != 50)
            {
                return;
            }

            FlatCat FlatCat = UberEnvironment.GetGame().GetNavigator().GetFlatCat(CategoryId);

            if (FlatCat == null)
            {
                return;
            }

            if (FlatCat.MinRank > Session.GetHabbo().Rank)
            {
                Session.SendNotif("You are not allowed to use this category. Your room has been moved to no category instead.");
                CategoryId = 0;
            }

            if (TagCount > 2)
            {
                return;
            }

            if (State < 0 || State > 2)
            {
                return;
            }

            if (_AllowPets == "65")
            {
                AllowPets = 1;
                Room.AllowPets = true;
            }
            else
            {
                Room.AllowPets = false;
            }

            if (_AllowPetsEat == "65")
            {
                AllowPetsEat = 1;
                Room.AllowPetsEating = true;
            }
            else
            {
                Room.AllowPetsEating = false;
            }

            if (_AllowWalkthrough == "65")
            {
                AllowWalkthrough = 1;
                Room.AllowWalkthrough = true;
            }
            else
            {
                Room.AllowWalkthrough = false;
            }

            if (_Hidewall == "65")
            {
                Hidewall = 1;
                Room.Hidewall = true;
            }
            else
            {
                Room.Hidewall = false;
            }

            Room.Name = Name;
            Room.State = State;
            Room.Description = Description;
            Room.Category = CategoryId;
            Room.Password = Password;
            Room.Tags = Tags;
            Room.UsersMax = MaxUsers;

            string formattedState = "open";

            if (Room.State == 1)
            {
                formattedState = "locked";
            }
            else if (Room.State > 1)
            {
                formattedState = "password";
            }

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.AddParamWithValue("caption", Room.Name);
                dbClient.AddParamWithValue("description", Room.Description);
                dbClient.AddParamWithValue("password", Room.Password);
                dbClient.AddParamWithValue("tags", formattedTags.ToString());
                dbClient.ExecuteQuery("UPDATE rooms SET caption = @caption, description = @description, password = @password, category = '" + CategoryId + "', state = '" + formattedState + "', tags = @tags, users_max = '" + MaxUsers + "', allow_pets = '" + AllowPets + "', allow_pets_eat = '" + AllowPetsEat + "', allow_walkthrough = '" + AllowWalkthrough + "', allow_hidewall = '" + Hidewall + "' WHERE id = '" + Room.RoomId + "' LIMIT 1");
            }

            GetResponse().Init(467);
            GetResponse().AppendUInt(Room.RoomId);
            SendResponse();

            GetResponse().Init(456);
            GetResponse().AppendUInt(Room.RoomId);
            SendResponse();

            GetResponse().Init(472);
            GetResponse().AppendBoolean(Room.Hidewall);
            Room.SendMessage(Response);

            RoomData Data = new RoomData();
            Data.Fill(Room);

            GetResponse().Init(454);
            GetResponse().AppendBoolean(false);
            Data.Serialize(GetResponse(), false);
            SendResponse();
        }

        private void GiveRights()
        {
            uint UserId = Request.PopWiredUInt();

            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);
            RoomUser RoomUser = Room.GetRoomUserByHabbo(UserId);

            if (Room == null || !Room.CheckRights(Session, true) || RoomUser == null || RoomUser.IsBot)
            {
                return;
            }

            if (RoomUser.GetClient().GetHabbo().HasFuse("fuse_mod"))
            {
                Session.SendNotif("You cannot give rights to this user!");
                return;
            }

            if (Room.UsersWithRights.Contains(UserId))
            {
                // todo: fix silly bug
                Session.SendNotif("User already has rights! (There appears to be a bug with the rights button, we are looking into it - for now rely on 'Advanced settings')");
                return;
            }

            Room.UsersWithRights.Add(UserId);

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("INSERT INTO room_rights (room_id,user_id) VALUES ('" + Room.RoomId + "','" + UserId + "')");
            }

            GetResponse().Init(510);
            GetResponse().AppendUInt(Room.RoomId);
            GetResponse().AppendUInt(UserId);
            GetResponse().AppendStringWithBreak(RoomUser.GetClient().GetHabbo().Username);
            SendResponse();

            RoomUser.AddStatus("flatcrtl", "");
            RoomUser.UpdateNeeded = true;

            RoomUser.GetClient().SendMessage(new ServerMessage(42));
        }

        private void TakeRights()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return;
            }

            StringBuilder DeleteParams = new StringBuilder();

            int Amount = Request.PopWiredInt32();

            for (int i = 0; i < Amount; i++)
            {
                if (i > 0)
                {
                    DeleteParams.Append(" OR ");
                }

                uint UserId = Request.PopWiredUInt();
                Room.UsersWithRights.Remove(UserId);
                DeleteParams.Append("room_id = '" + Room.RoomId + "' AND user_id = '" + UserId + "'");

                RoomUser User = Room.GetRoomUserByHabbo(UserId);

                if (User != null && !User.IsBot)
                {
                    User.GetClient().SendMessage(new ServerMessage(43));
                }

                GetResponse().Init(511);
                GetResponse().AppendUInt(Room.RoomId);
                GetResponse().AppendUInt(UserId);
                SendResponse();
            }

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("DELETE FROM room_rights WHERE " + DeleteParams.ToString());
            }
        }

        private void TakeAllRights()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return;
            }

            foreach (uint UserId in Room.UsersWithRights)
            {
                RoomUser User = Room.GetRoomUserByHabbo(UserId);

                if (User != null && !User.IsBot)
                {
                    User.GetClient().SendMessage(new ServerMessage(43));
                }

                GetResponse().Init(511);
                GetResponse().AppendUInt(Room.RoomId);
                GetResponse().AppendUInt(UserId);
                SendResponse();
            }

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("DELETE FROM room_rights WHERE room_id = '" + Room.RoomId + "'");
            }

            Room.UsersWithRights = null;
            Room.UsersWithRights = new List<uint>();
        }

        private void KickUser()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            if (!Room.CheckRights(Session))
            {
                return; // insufficient permissions
            }

            uint UserId = Request.PopWiredUInt();
            RoomUser User = Room.GetRoomUserByHabbo(UserId);

            if (User == null || User.IsBot)
            {
                return;
            }

            if (Room.CheckRights(User.GetClient(), true) || User.GetClient().GetHabbo().HasFuse("fuse_mod"))
            {
                Session.SendNotif("You can't kick the Room owner or mods/admins!");
                return; // can't kick room owner or mods!
            }

            User.CanWalk = false;
            User.MoveTo(Room.Model.DoorX, Room.Model.DoorY);
        }

        private void BanUser()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return; // insufficient permissions
            }

            uint UserId = Request.PopWiredUInt();
            RoomUser User = Room.GetRoomUserByHabbo(UserId);

            if (User == null || User.IsBot)
            {
                return;
            }

            if (User.GetClient().GetHabbo().HasFuse("fuse_mod"))
            {
                return;
            }

            Room.AddBan(UserId);
            Room.RemoveUserFromRoom(User.GetClient(), true, true);
        }

        private void SetHomeRoom()
        {
            uint RoomId = Request.PopWiredUInt();
            RoomData Data = UberEnvironment.GetGame().GetRoomManager().GenerateRoomData(RoomId);

            if (RoomId != 0)
            {
                if (Data == null || Data.Owner.ToLower() != Session.GetHabbo().Username.ToLower())
                {
                    return;
                }
            }

            Session.GetHabbo().HomeRoom = RoomId;

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("UPDATE users SET home_room = '" + RoomId + "' WHERE id = '" + Session.GetHabbo().Id + "' LIMIT 1");
            }

            GetResponse().Init(455);
            GetResponse().AppendUInt(RoomId);
            SendResponse();
        }

        private void DeleteRoom()
        {
            uint RoomId = Request.PopWiredUInt();
            RoomData Data = UberEnvironment.GetGame().GetRoomManager().GenerateRoomData(RoomId);

            if (Data == null || Data.Owner.ToLower() != Session.GetHabbo().Username.ToLower())
            {
                return;
            }

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("DELETE FROM rooms WHERE id = '" + Data.Id + "' LIMIT 1");
                dbClient.ExecuteQuery("DELETE FROM user_favorites WHERE room_id = '" + Data.Id + "'");
                dbClient.ExecuteQuery("DELETE FROM items WHERE room_id = '" + Data.Id + "'");
                dbClient.ExecuteQuery("DELETE FROM room_rights WHERE room_id = '" + Data.Id + "'");
                dbClient.ExecuteQuery("UPDATE users SET home_room = '0' WHERE home_room = '" + Data.Id + "'");
                dbClient.ExecuteQuery("UPDATE user_pets SET room_id = '0' WHERE room_id = '" + Data.Id + "'");
            }

            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Data.Id);

            if (Room != null)
            {
                for (int i = 0; i < Room.UserList.Count; i++)
                {
                    RoomUser User = Room.UserList[i];

                    if (User.IsBot)
                    {
                        continue;
                    }

                    User.GetClient().SendMessage(new ServerMessage(18));
                    User.GetClient().GetHabbo().OnLeaveRoom();
                }

                UberEnvironment.GetGame().GetRoomManager().UnloadRoom(Data.Id);
            }

            GetResponse().Init(101);
            SendResponse();

            Session.SendMessage(UberEnvironment.GetGame().GetNavigator().SerializeRoomListing(Session, -3));

            // Refresh pets and items inventory
            //Session.GetHabbo().GetInventoryComponent().UpdateItems(true); // obselete at the moment, doesn't return items to inventory!
            Session.GetHabbo().GetInventoryComponent().UpdatePets(true);
        }

        private void LookAt()
        {
          /*  Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Session.GetHabbo().Id);

            if (User == null)
            {
                return;
            }

            int X = Request.PopWiredInt32();
            int Y = Request.PopWiredInt32();

            if (X == User.X && Y == User.Y)
            {
                return;
            }

            int Rot = Rotation.Calculate(User.X, User.Y, X, Y);

            User.SetRot(Rot);*/
        }

        private void StartTyping()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Session.GetHabbo().Id);

            if (User == null)
            {
                return;
            }

            ServerMessage Message = new ServerMessage(361);
            Message.AppendInt32(User.VirtualId);
            Message.AppendBoolean(true);
            Room.SendMessage(Message);
        }

        private void StopTyping()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Session.GetHabbo().Id);

            if (User == null)
            {
                return;
            }

            ServerMessage Message = new ServerMessage(361);
            Message.AppendInt32(User.VirtualId);
            Message.AppendBoolean(false);
            Room.SendMessage(Message);
        }

        private void IgnoreUser()
        {
            Room Room = Session.GetHabbo().CurrentRoom;

            if (Room == null)
            {
                return;
            }

            string UserName = Request.PopFixedString();

            RoomUser IgnoreUser = Room.GetRoomUserByHabbo(UserName);

            if (IgnoreUser == null)
            {
                return;
            }

            uint IgnoreId = IgnoreUser.GetClient().GetHabbo().Id;

            if (Session.GetHabbo().MutedUsers.Contains(IgnoreId))
            {
                return;
            }

            Session.GetHabbo().MutedUsers.Add(IgnoreId);

            GetResponse().Init(419);
            GetResponse().AppendBoolean(true);
            SendResponse();
        }

        private void UnignoreUser()
        {
            Room Room = Session.GetHabbo().CurrentRoom;

            if (Room == null)
            {
                return;
            }

            string UserName = Request.PopFixedString();

            RoomUser IgnoreUser = Room.GetRoomUserByHabbo(UserName);

            if (IgnoreUser == null)
            {
                return;
            }

            uint IgnoreId = IgnoreUser.GetClient().GetHabbo().Id;

            if (!Session.GetHabbo().MutedUsers.Contains(IgnoreId))
            {
                return;
            }

            Session.GetHabbo().MutedUsers.Remove(IgnoreId);

            GetResponse().Init(419);
            GetResponse().AppendBoolean(false);
            SendResponse();
        }

        private void CanCreateRoomEvent()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return;
            }

            Boolean Allow = true;
            int ErrorCode = 0;

            if (Room.State != 0)
            {
                Allow = false;
                ErrorCode = 3;
            }

            GetResponse().Init(367);
            GetResponse().AppendBoolean(Allow);
            GetResponse().AppendInt32(ErrorCode);
            SendResponse();
        }

        private void StartEvent()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true) || Room.Event != null || Room.State != 0)
            {
                return;
            }

            int category = Request.PopWiredInt32();
            string name = UberEnvironment.FilterInjectionChars(Request.PopFixedString());
            string descr = UberEnvironment.FilterInjectionChars(Request.PopFixedString());
            int tagCount = Request.PopWiredInt32();

            Room.Event = new RoomEvent(Room.RoomId, name, descr, category, null);
            Room.Event.Tags = new SafeList<string>();

            for (int i = 0; i < tagCount; i++)
            {
                Room.Event.Tags.Add(UberEnvironment.FilterInjectionChars(Request.PopFixedString()));
            }

            Room.SendMessage(Room.Event.Serialize(Session));
        }

        private void StopEvent()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true) || Room.Event == null)
            {
                return;
            }

            Room.Event = null;

            ServerMessage Message = new ServerMessage(370);
            Message.AppendStringWithBreak("-1");
            Room.SendMessage(Message);
        }

        private void EditEvent()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true) || Room.Event == null)
            {
                return;
            }

            int category = Request.PopWiredInt32();
            string name = UberEnvironment.FilterInjectionChars(Request.PopFixedString());
            string descr = UberEnvironment.FilterInjectionChars(Request.PopFixedString());
            int tagCount = Request.PopWiredInt32();

            Room.Event.Category = category;
            Room.Event.Name = name;
            Room.Event.Description = descr;
            Room.Event.Tags = new SafeList<string>();

            for (int i = 0; i < tagCount; i++)
            {
                Room.Event.Tags.Add(UberEnvironment.FilterInjectionChars(Request.PopFixedString()));
            }

            Room.SendMessage(Room.Event.Serialize(Session));
        }

        private void Wave()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Session.GetHabbo().Id);

            if (User == null)
            {
                return;
            }

            User.Unidle();

            User.DanceId = 0;

            ServerMessage Message = new ServerMessage(481);
            Message.AppendInt32(User.VirtualId);
            Room.SendMessage(Message);

            if (Session.GetHabbo().CurrentQuestId == 8)
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(8, Session);
            }
        }

        private void GetUserTags()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Request.PopWiredUInt());

            if (User == null || User.IsBot)
            {
                return;
            }

            GetResponse().Init(350);
            GetResponse().AppendUInt(User.GetClient().GetHabbo().Id);
            GetResponse().AppendInt32(User.GetClient().GetHabbo().Tags.Count);

            foreach (string Tag in User.GetClient().GetHabbo().Tags)
            {
                GetResponse().AppendStringWithBreak(Tag);
            }

            SendResponse();
        }

        private void GetUserBadges()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Request.PopWiredUInt());

            if (User == null || User.IsBot || User.GetClient() == null || User.GetClient().GetHabbo() == null)
            {
                return;
            }

            // CdjUzYZJIACH_RespectEarned1JACH_EmailVerification1E^jUzYZH

            GetResponse().Init(228);
            GetResponse().AppendUInt(User.GetClient().GetHabbo().Id);
            GetResponse().AppendInt32(User.GetClient().GetHabbo().GetBadgeComponent().EquippedCount);

            foreach (Badge Badge in User.GetClient().GetHabbo().GetBadgeComponent().BadgeList)
            {
                if (Badge.Slot <= 0)
                {
                    continue;
                }

                GetResponse().AppendInt32(Badge.Slot);
                GetResponse().AppendStringWithBreak(Badge.Code);
            }

            SendResponse();
        }

        private void RateRoom()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || Session.GetHabbo().RatedRooms.Contains(Room.RoomId) || Room.CheckRights(Session, true))
            {
                return;
            }

            int Rating = Request.PopWiredInt32();

            switch (Rating)
            {
                case -1:

                    Room.Score--;
                    break;

                case 1:

                    Room.Score++;
                    break;

                default:

                    return;
            }

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("UPDATE rooms SET score = '" + Room.Score + "' WHERE id = '" + Room.RoomId + "' LIMIT 1");
            }

            Session.GetHabbo().RatedRooms.Add(Room.RoomId);

            GetResponse().Init(345);
            GetResponse().AppendInt32(Room.Score);
            SendResponse();
        }

        private void Dance()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Session.GetHabbo().Id);

            if (User == null)
            {
                return;
            }

            User.Unidle();

            int DanceId = Request.PopWiredInt32();

            if (DanceId < 0 || DanceId > 4 || (!Session.GetHabbo().HasFuse("fuse_use_club_dance") && DanceId > 1))
            {
                DanceId = 0;
            }

            if (DanceId > 0 && User.CarryItemID > 0)
            {
                User.CarryItem(0);
            }

            User.DanceId = DanceId;

            ServerMessage DanceMessage = new ServerMessage(480);
            DanceMessage.AppendInt32(User.VirtualId);
            DanceMessage.AppendInt32(DanceId);
            Room.SendMessage(DanceMessage);

            if (Session.GetHabbo().CurrentQuestId == 6)
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(6, Session);
            }
        }

        private void AnswerDoorbell()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session))
            {
                return;
            }

            string Name = Request.PopFixedString();
            byte[] Result = Request.ReadBytes(1);

            GameClient Client = UberEnvironment.GetGame().GetClientManager().GetClientByHabbo(Name);

            if (Client == null)
            {
                return;
            }

            if (Result[0] == Convert.ToByte(65))
            {
                Client.GetHabbo().LoadingChecksPassed = true;

                Client.GetMessageHandler().GetResponse().Init(41);
                Client.GetMessageHandler().SendResponse();
            }
            else
            {
                Client.GetMessageHandler().GetResponse().Init(131);
                Client.GetMessageHandler().SendResponse();
            }
        }

        private void ApplyRoomEffect()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return;
            }

            UserItem Item = Session.GetHabbo().GetInventoryComponent().GetItem(Request.PopWiredUInt());

            if (Item == null)
            {
                return;
            }

            string type = "floor";

            if (Item.GetBaseItem().Name.ToLower().Contains("wallpaper"))
            {
                type = "wallpaper";
            }
            else if (Item.GetBaseItem().Name.ToLower().Contains("landscape"))
            {
                type = "landscape";
            }

            switch (type)
            {
                case "floor":

                    Room.Floor = Item.ExtraData;

                    if (Session.GetHabbo().CurrentQuestId == 13)
                    {
                        UberEnvironment.GetGame().GetQuestManager().UpdateQuest(13, Session);
                    }

                    break;

                case "wallpaper":

                    Room.Wallpaper = Item.ExtraData;

                    if (Session.GetHabbo().CurrentQuestId == 11)
                    {
                        UberEnvironment.GetGame().GetQuestManager().UpdateQuest(11, Session);
                    }

                    break;

                case "landscape":

                    Room.Landscape = Item.ExtraData;
                    break;
            }

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("UPDATE rooms SET " + type + " = '" + Item.ExtraData + "' WHERE id = '" + Room.RoomId + "' LIMIT 1");
            }

            Session.GetHabbo().GetInventoryComponent().RemoveItem(Item.Id, true);

            ServerMessage Message = new ServerMessage(46);
            Message.AppendStringWithBreak(type);
            Message.AppendStringWithBreak(Item.ExtraData);
            Room.SendMessage(Message);
        }

        private void PlaceSticky()
        {
            //rvRN@P:w=5,0 l=15,31 r
            // uint RoomItemId
            // Boolean junk?

            uint ItemId = Request.PopWiredUInt();
            bool Junk = Request.PopWiredBoolean();
            string Pos = Request.PopFixedString();

            UserItem InventoryItem = Session.GetHabbo().GetInventoryComponent().GetItem(ItemId);

            if (InventoryItem == null)
            {
                return;
            }

            if (InventoryItem.GetBaseItem().Type != "i")
            {
                return;
            }

            Room Room = Session.GetHabbo().CurrentRoom;

            if (Room == null)
            {
                return;
            }

            string WallPos = Room.WallPositionCheck(":" + Pos);

            if (WallPos == null)
            {
                return;
            }

            RoomItem RoomItem = new RoomItem(ItemId, Room.RoomId, InventoryItem.BaseItem, InventoryItem.ExtraData, 0, 0, 0.0, 0, WallPos, 0);

            if (Room.SetWallItem(Session, RoomItem))
            {
                Session.GetHabbo().GetInventoryComponent().RemoveItem(ItemId, false);
            }

            Console.WriteLine("ItemId = " + ItemId + " Pos = " + Pos);
        }

        private void PlaceItem()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session))
            {
                return;
            }

            string PlacementData = Request.PopFixedString();
            string[] DataBits = PlacementData.Split(' ');

            if (DataBits[0].Contains("-"))
            {
                DataBits[0] = DataBits[0].Replace("-", "");
            }

            uint ItemId = uint.Parse(DataBits[0]);

            UserItem Item = Session.GetHabbo().GetInventoryComponent().GetItem(ItemId);

            if (Item == null)
            {
                return;
            }

            switch (Item.GetBaseItem().InteractionType.ToLower())
            {
                case "dimmer":

                    if (Room.ItemCountByType("dimmer") >= 1)
                    {
                        Session.SendNotif("You can only have one moodlight in a room.");
                        return;
                    }

                    break;
            }

            // Wall Item
            if (DataBits[1].StartsWith(":"))
            {
                string WallPos = Room.WallPositionCheck(":" + PlacementData.Split(':')[1]);

                if (WallPos == null)
                {
                    GetResponse().Init(516);
                    GetResponse().AppendInt32(11);
                    SendResponse();

                    return;
                }

                RoomItem RoomItem = new RoomItem(Item.Id, Room.RoomId, Item.BaseItem, Item.ExtraData, 0, 0, 0.0, 0, WallPos,0);

                if (Room.SetWallItem(Session, RoomItem))
                {
                    Session.GetHabbo().GetInventoryComponent().RemoveItem(ItemId, false);
                }
            }
            // Floor Item
            else
            {
                int X = int.Parse(DataBits[1]);
                int Y = int.Parse(DataBits[2]);
                int Rot = int.Parse(DataBits[3]);

                RoomItem RoomItem = new RoomItem(Item.Id, Room.RoomId, Item.BaseItem, Item.ExtraData, 0, 0, 0, 0, "", 0);

                if (Room.SetFloorItem(Session, RoomItem, X, Y, Rot, true))
                {
                    Session.GetHabbo().GetInventoryComponent().RemoveItem(ItemId, false);
                }
            }

            if (Session.GetHabbo().CurrentQuestId == 14)
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(14, Session);
            }
        }

        private void TakeItem()
        {
            int junk = Request.PopWiredInt32();

            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return;
            }

            RoomItem Item = Room.GetItem(Request.PopWiredUInt());

            if (Item == null)
            {
                return;
            }

            switch (Item.GetBaseItem().InteractionType.ToLower())
            {
                case "postit":

                    return; // not allowed to pick up post.its
            }

            if (Session.GetHabbo().CurrentQuestId == 10)
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(10, Session);
            }

            Room.RemoveFurniture(Session, Item.Id, false);
            Session.GetHabbo().GetInventoryComponent().AddItem(Item.Id, Item.BaseItem, Item.ExtraData, false);
            Session.GetHabbo().GetInventoryComponent().UpdateItems(false);
        }

        private void MoveItem()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session))
            {
                return;
            }

            RoomItem Item = Room.GetItem(Request.PopWiredUInt());

            if (Item == null)
            {
                return;
            }

            int x = Request.PopWiredInt32();
            int y = Request.PopWiredInt32();
            int Rotation = Request.PopWiredInt32();
            int Junk = Request.PopWiredInt32();

            if (x > Room.Model.MapSizeX || y > Room.Model.MapSizeY)
            {
                Session.SendNotif("You can't place furniture outside the map!");
                return;
            }

            if (x == Item.X && y == Item.Y && Rotation == Item.Rot)
            {
                return; // useless handling :D
            }

            if (Session.GetHabbo().CurrentQuestId == 1 && (x != Item.X || y != Item.Y))
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(1, Session);
            }

            if (Session.GetHabbo().CurrentQuestId == 7 && Rotation != Item.Rot)
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(7, Session);
            }

            if (Session.GetHabbo().CurrentQuestId == 9 && Item.Z >= 0.1)
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(9, Session);
            }

            Room.SetFloorItem(Session, Item, x, y, Rotation, false);
        }

        private void TriggerItem()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomItem Item = Room.GetItem(Request.PopWiredUInt());

            if (Item == null)
            {
                return;
            }

            Boolean hasRights = false;

            if (Room.CheckRights(Session))
            {
                hasRights = true;
            }

            Item.Interactor.OnTrigger(Session, Item, Request.PopWiredInt32(), hasRights);

            if (Session.GetHabbo().CurrentQuestId == 12)
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(12, Session);
            }

            if (Session.GetHabbo().CurrentQuestId == 18 && Item.GetBaseItem().Name == "bw_lgchair")
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(18, Session);
            }

            if (Session.GetHabbo().CurrentQuestId == 20 && Item.GetBaseItem().Name.Contains("bw_sboard"))
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(20, Session);
            }

            if (Session.GetHabbo().CurrentQuestId == 21 && Item.GetBaseItem().Name.Contains("bw_van"))
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(21, Session);
            }

            if (Session.GetHabbo().CurrentQuestId == 22 && Item.GetBaseItem().Name.Contains("party_floor"))
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(22, Session);
            }

            if (Session.GetHabbo().CurrentQuestId == 23 && Item.GetBaseItem().Name.Contains("party_ball"))
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(23, Session);
            }

            if (Session.GetHabbo().CurrentQuestId == 24 && Item.GetBaseItem().Name.Contains("jukebox"))
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(24, Session);
            }
        }

        private void TriggerItemDiceSpecial()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomItem Item = Room.GetItem(Request.PopWiredUInt());

            if (Item == null)
            {
                return;
            }

            Boolean hasRights = false;

            if (Room.CheckRights(Session))
            {
                hasRights = true;
            }

            Item.Interactor.OnTrigger(Session, Item, -1, hasRights);
        }

        private void OpenPostit()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomItem Item = Room.GetItem(Request.PopWiredUInt());

            if (Item == null || Item.GetBaseItem().InteractionType.ToLower() != "postit")
            {
                return;
            }

            GetResponse().Init(48);
            GetResponse().AppendStringWithBreak(Item.Id.ToString());
            GetResponse().AppendStringWithBreak(Item.ExtraData);
            SendResponse();
        }

        private void SavePostit()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            RoomItem Item = Room.GetItem(Request.PopWiredUInt());

            if (Item == null || Item.GetBaseItem().InteractionType.ToLower() != "postit")
            {
                return;
            }

            string Data = Request.PopFixedString();
            string Color = Data.Split(' ')[0];
            string Text = UberEnvironment.FilterInjectionChars(Data.Substring(Color.Length + 1), true);

            if (!Room.CheckRights(Session))
            {
                if (!Data.StartsWith(Item.ExtraData))
                {
                    return; // we can only ADD stuff! older stuff changed, this is not allowed
                }
            }

            switch (Color)
            {
                case "FFFF33":
                case "FF9CFF":
                case "9CCEFF":
                case "9CFF9C":

                    break;

                default:

                    return; // invalid color
            }

            Item.ExtraData = Color + " " + Text;
            Item.UpdateState(true, true);
            Item.UpdateNeeded = true;
        }

        private void DeletePostit()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return;
            }

            RoomItem Item = Room.GetItem(Request.PopWiredUInt());

            if (Item == null || Item.GetBaseItem().InteractionType.ToLower() != "postit")
            {
                return;
            }

            Room.RemoveFurniture(Session, Item.Id, true);
        }

        private void OpenPresent()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return;
            }

            RoomItem Present = Room.GetItem(Request.PopWiredUInt());

            if (Present == null)
            {
                return;
            }

            DataRow Data = null;

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                Data = dbClient.ReadDataRow("SELECT base_id,amount,extra_data FROM user_presents WHERE item_id = '" + Present.Id + "' LIMIT 1");
            }

            if (Data == null)
            {
                return;
            }

            Item BaseItem = UberEnvironment.GetGame().GetItemManager().GetItem((uint)Data["base_id"]);

            if (BaseItem == null)
            {
                return;
            }

            Room.RemoveFurniture(Session, Present.Id, true);

            GetResponse().Init(219);
            GetResponse().AppendUInt(Present.Id);
            SendResponse();

            GetResponse().Init(129);
            GetResponse().AppendStringWithBreak(BaseItem.Type);
            GetResponse().AppendInt32(BaseItem.SpriteId);
            GetResponse().AppendStringWithBreak(BaseItem.Name);
            SendResponse();

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("DELETE FROM user_presents WHERE item_id = '" + Present.Id + "' LIMIT 1");
            }

            UberEnvironment.GetGame().GetCatalog().DeliverItems(Session, BaseItem, (int)Data["amount"], (String)Data["extra_data"]);
        }

        private void GetMoodlight()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true) || Room.MoodlightData == null)
            {
                return;
            }

            GetResponse().Init(365);
            GetResponse().AppendInt32(Room.MoodlightData.Presets.Count);
            GetResponse().AppendInt32(Room.MoodlightData.CurrentPreset);

            int i = 0;

            /*for (int e = 0; e < Room.MoodlightData.Presets.Count; e++)
            {
                MoodlightPreset Preset = Room.MoodlightData.Presets[e];

                if (Preset != null)
                {
                    GetResponse().AppendInt32(i);
                    GetResponse().AppendInt32(int.Parse(UberEnvironment.BoolToEnum(Preset.BackgroundOnly)) + 1);
                    GetResponse().AppendStringWithBreak(Preset.ColorCode);
                    GetResponse().AppendInt32(Preset.ColorIntensity);
                }
            }*/

            foreach (MoodlightPreset Preset in Room.MoodlightData.Presets)
            {
                i++;

                GetResponse().AppendInt32(i);
                GetResponse().AppendInt32(int.Parse(UberEnvironment.BoolToEnum(Preset.BackgroundOnly)) + 1);
                GetResponse().AppendStringWithBreak(Preset.ColorCode);
                GetResponse().AppendInt32(Preset.ColorIntensity);
            }

            SendResponse();
        }

        private void UpdateMoodlight()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true) || Room.MoodlightData == null)
            {
                return;
            }

            RoomItem Item = null;

            for(int i = 0; i < Room.Items.Count; i++)
            {
                RoomItem I = Room.Items[i]; if (I == null) { return; }

                if (I.GetBaseItem().InteractionType.ToLower() == "dimmer")
                {
                    Item = I;
                    break;
                }
            }

            if (Item == null)
            {
                return;
            }

            // EVIH@G#EA4532RbI

            int Preset = Request.PopWiredInt32();
            int BackgroundMode = Request.PopWiredInt32();
            string ColorCode = Request.PopFixedString();
            int Intensity = Request.PopWiredInt32();

            bool BackgroundOnly = false;

            if (BackgroundMode >= 2)
            {
                BackgroundOnly = true;
            }

            Room.MoodlightData.Enabled = true;
            Room.MoodlightData.CurrentPreset = Preset;
            Room.MoodlightData.UpdatePreset(Preset, ColorCode, Intensity, BackgroundOnly);

            Item.ExtraData = Room.MoodlightData.GenerateExtraData();
            Item.UpdateState();
        }

        private void SwitchMoodlightStatus()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true) || Room.MoodlightData == null)
            {
                return;
            }

            RoomItem Item = null;

            for (int i = 0; i < Room.Items.Count; i++)
            {
                RoomItem I = Room.Items[i]; if (I == null) { return; }

                if (I.GetBaseItem().InteractionType.ToLower() == "dimmer")
                {
                    Item = I;
                    break;
                }
            }

            if (Item == null)
            {
                return;
            }

            if (Room.MoodlightData.Enabled)
            {
                Room.MoodlightData.Disable();
            }
            else
            {
                Room.MoodlightData.Enable();
            }

            Item.ExtraData = Room.MoodlightData.GenerateExtraData();
            Item.UpdateState();
        }

        private void InitTrade()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CanTradeInRoom)
            {
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Session.GetHabbo().Id);
            RoomUser User2 = Room.GetRoomUserByVirtualId(Request.PopWiredInt32());

            if (User == null || User2 == null)
            {
                return; // useless trade :D
            }

            Room.TryStartTrade(User, User2);
        }

        private void OfferTradeItem()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CanTradeInRoom)
            {
                return;
            }

            Trade Trade = Room.GetUserTrade(Session.GetHabbo().Id);
            UserItem Item = Session.GetHabbo().GetInventoryComponent().GetItem(Request.PopWiredUInt());

            if (Trade == null || Item == null)
            {
                return;
            }

            Trade.OfferItem(Session.GetHabbo().Id, Item);
        }

        private void TakeBackTradeItem()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CanTradeInRoom)
            {
                return;
            }

            Trade Trade = Room.GetUserTrade(Session.GetHabbo().Id);
            UserItem Item = Session.GetHabbo().GetInventoryComponent().GetItem(Request.PopWiredUInt());

            if (Trade == null || Item == null)
            {
                return;
            }

            Trade.TakeBackItem(Session.GetHabbo().Id, Item);
        }

        private void StopTrade()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CanTradeInRoom)
            {
                return;
            }

            Room.TryStopTrade(Session.GetHabbo().Id);
        }

        private void AcceptTrade()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CanTradeInRoom)
            {
                return;
            }

            Trade Trade = Room.GetUserTrade(Session.GetHabbo().Id);

            if (Trade == null)
            {
                return;
            }

            Trade.Accept(Session.GetHabbo().Id);
        }

        private void UnacceptTrade()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CanTradeInRoom)
            {
                return;
            }

            Trade Trade = Room.GetUserTrade(Session.GetHabbo().Id);

            if (Trade == null)
            {
                return;
            }

            Trade.Unaccept(Session.GetHabbo().Id);
        }

        private void CompleteTrade()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CanTradeInRoom)
            {
                return;
            }

            Trade Trade = Room.GetUserTrade(Session.GetHabbo().Id);

            if (Trade == null)
            {
                return;
            }

            Trade.CompleteTrade(Session.GetHabbo().Id);
        }

        private void GiveRespect()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || Session.GetHabbo().DailyRespectPoints <= 0)
            {
                return;
            }

            RoomUser User = Room.GetRoomUserByHabbo(Request.PopWiredUInt());

            if (User == null || User.GetClient().GetHabbo().Id == Session.GetHabbo().Id || User.IsBot)
            {
                return;
            }

            if (Session.GetHabbo().CurrentQuestId == 5)
            {
                UberEnvironment.GetGame().GetQuestManager().UpdateQuest(5, Session);
            }

            Session.GetHabbo().RespectGiven++;
            Session.GetHabbo().DailyRespectPoints--;
            User.GetClient().GetHabbo().Respect++;

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("UPDATE users SET respect = respect + 1 WHERE id = '" + User.GetClient().GetHabbo().Id + "' LIMIT 1");
                dbClient.ExecuteQuery("UPDATE users SET daily_respect_points = daily_respect_points - 1, respect_given = respect_given + 1 WHERE id = '" + Session.GetHabbo().Id + "' LIMIT 1");
            }

            #region Respect Achievements
            int RespectGiven = Session.GetHabbo().RespectGiven;

            if (RespectGiven >= 100) { User.GetClient().UnlockAchievement(16, 1); }

            int Respect = User.GetClient().GetHabbo().Respect;

            if (Respect >= 1) { User.GetClient().UnlockAchievement(15, 1); }
            if (Respect >= 6) { User.GetClient().UnlockAchievement(15, 2); }
            if (Respect >= 16) { User.GetClient().UnlockAchievement(15, 3); }
            if (Respect >= 66) { User.GetClient().UnlockAchievement(15, 4); }
            if (Respect >= 166) { User.GetClient().UnlockAchievement(15, 5); }
            if (Respect >= 366) { User.GetClient().UnlockAchievement(15, 6); }
            if (Respect >= 566) { User.GetClient().UnlockAchievement(15, 7); }
            if (Respect >= 766) { User.GetClient().UnlockAchievement(15, 8); }
            if (Respect >= 966) { User.GetClient().UnlockAchievement(15, 9); }
            if (Respect >= 1166) { User.GetClient().UnlockAchievement(15, 10); }
            #endregion

            ServerMessage Message = new ServerMessage(440);
            Message.AppendUInt(User.GetClient().GetHabbo().Id);
            Message.AppendInt32(User.GetClient().GetHabbo().Respect);
            Room.SendMessage(Message);
        }

        private void ApplyEffect()
        {
            Session.GetHabbo().GetAvatarEffectsInventoryComponent().ApplyEffect(Request.PopWiredInt32());
        }

        private void EnableEffect()
        {
            Session.GetHabbo().GetAvatarEffectsInventoryComponent().EnableEffect(Request.PopWiredInt32());
        }

        private void RecycleItems()
        {
            if (!Session.GetHabbo().InRoom)
            {
                return;
            }

            int itemCount = Request.PopWiredInt32();

            if (itemCount != 5)
            {
                return;
            }

            for (int i = 0; i < itemCount; i++)
            {
                UserItem Item = Session.GetHabbo().GetInventoryComponent().GetItem(Request.PopWiredUInt());

                if (Item != null && Item.GetBaseItem().AllowRecycle)
                {
                    Session.GetHabbo().GetInventoryComponent().RemoveItem(Item.Id, true);
                }
                else
                {
                    return;
                }
            }

            uint newItemId = UberEnvironment.GetGame().GetCatalog().GenerateItemId();
            EcotronReward Reward = UberEnvironment.GetGame().GetCatalog().GetRandomEcotronReward();

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("INSERT INTO items (id,user_id,room_id,base_item,extra_data,x,y,z,rot,wall_pos) VALUES ('" + newItemId + "','" + Session.GetHabbo().Id + "','0','1478','" + DateTime.Now.ToLongDateString() + "','0','0','0','0','')");
                dbClient.ExecuteQuery("INSERT INTO user_presents (item_id,base_id,amount,extra_data) VALUES ('" + newItemId + "','" + Reward.BaseId + "','1','')");
            }

            Session.GetHabbo().GetInventoryComponent().UpdateItems(true);

            GetResponse().Init(508);
            GetResponse().AppendBoolean(true);
            GetResponse().AppendUInt(newItemId);
            SendResponse();
        }

        private void RedeemExchangeFurni()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return;
            }

            RoomItem Exchange = Room.GetItem(Request.PopWiredUInt());

            if (Exchange == null)
            {
                return;
            }

            if (!Exchange.GetBaseItem().Name.StartsWith("CF_") && !Exchange.GetBaseItem().Name.StartsWith("CFC_"))
            {
                return;
            }

            string[] Split = Exchange.GetBaseItem().Name.Split('_');
            int Value = int.Parse(Split[1]);

            if (Value > 0)
            {
                Session.GetHabbo().Credits += Value;
                Session.GetHabbo().UpdateCreditsBalance(true);
            }

            Room.RemoveFurniture(null, Exchange.Id, true);

            GetResponse().Init(219);
            SendResponse();
        }

        private void EnterInfobus()
        {
            // AQThe Infobus is currently closed.
            GetResponse().Init(81);
            GetResponse().AppendStringWithBreak("The Infobus is not yet in use.");
            SendResponse();
        }

        private void KickBot()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || !Room.CheckRights(Session, true))
            {
                return;
            }

            RoomUser Bot = Room.GetRoomUserByVirtualId(Request.PopWiredInt32());

            if (Bot == null || !Bot.IsBot)
            {
                return;
            }

            Room.RemoveBot(Bot.VirtualId, true);
        }

        private void PlacePet()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || (!Room.AllowPets && !Room.CheckRights(Session, true)))
            {
                return;
            }

            uint PetId = Request.PopWiredUInt();

            Pet Pet = Session.GetHabbo().GetInventoryComponent().GetPet(PetId);

            if (Pet == null || Pet.PlacedInRoom)
            {
                return;
            }

            int X = Request.PopWiredInt32();
            int Y = Request.PopWiredInt32();

            if (X < 0 || Y < 0)
            {
                return; // invalid y || x
            }

            if (X > Room.Model.MapSizeX || Y > Room.Model.MapSizeY)
            {
                Session.SendNotif("You can't place a pet outside the map!");
                return; // invalid y || x
            }

            if (!Room.CanWalk(X, Y, 0, true,Session))
            {
                return;
            }

            if (Room.PetCount >= UberEnvironment.GetGame().GetRoomManager().MAX_PETS_PER_ROOM)
            {
                Session.SendNotif("There are too many pets in this room. A room may only contain up to " + UberEnvironment.GetGame().GetRoomManager().MAX_PETS_PER_ROOM + " pets.");
                return;
            }

            Pet.PlacedInRoom = true;
            Pet.RoomId = Room.RoomId;

            RoomUser PetUser = Room.DeployBot(new RoomBot(Pet.PetId, Pet.RoomId, "pet", "freeroam", Pet.Name, "", Pet.Look, X, Y, 0, 0, 0, 0, 0, 0), Pet);

            if (Room.CheckRights(Session, true))
            {
                Session.GetHabbo().GetInventoryComponent().MovePetToRoom(Pet.PetId, Room.RoomId);
            }
        }

        private void GetPetInfo()
        {
            uint PetId = Request.PopWiredUInt();

            Room Room = Session.GetHabbo().CurrentRoom;

            if (Room == null)
            {
                return;
            }

            RoomUser Pet = null;

            if ((Pet = Room.GetPet(PetId)) == null)
            {
                DataRow Row = null;

                using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                {
                    dbClient.AddParamWithValue("petid", PetId);
                    Row = dbClient.ReadDataRow("SELECT * FROM user_pets WHERE id = @petid LIMIT 1");
                }

                if (Row == null)
                {
                    return;
                }

                Session.SendMessage(UberEnvironment.GetGame().GetCatalog().GeneratePetFromRow(Row).SerializeInfo());
            }
            else
            {
                Session.SendMessage(Pet.PetData.SerializeInfo());
            }
        }

        private void PickUpPet()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null || Room.IsPublic || (!Room.AllowPets && !Room.CheckRights(Session, true)))
            {
                return;
            }

            uint PetId = Request.PopWiredUInt();
            RoomUser PetUser = Room.GetPet(PetId);

            if (PetUser == null || PetUser.PetData == null || PetUser.PetData.OwnerId != Session.GetHabbo().Id)
            {
                return;
            }

            Session.GetHabbo().GetInventoryComponent().AddPet(PetUser.PetData);
            Room.RemoveBot(PetUser.VirtualId, false);
        }

        private void RespectPet()
        {
            Room Room = UberEnvironment.GetGame().GetRoomManager().GetRoom(Session.GetHabbo().CurrentRoomId);

            if (Room == null)
            {
                return;
            }

            uint PetId = Request.PopWiredUInt();
            RoomUser PetUser = Room.GetPet(PetId);

            if (PetUser == null || PetUser.PetData == null || !PetUser.IsPet)
            {
                return;
            }

            PetUser.PetData.OnRespect();
            Session.GetHabbo().DailyPetRespectPoints--;

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.AddParamWithValue("userid", Session.GetHabbo().Id);
                dbClient.ExecuteQuery("UPDATE users SET daily_pet_respect_points = daily_pet_respect_points - 1 WHERE id = @userid LIMIT 1");
            }
        }

        private void GetJukeboxSongs()
        {
            // todo
        }

        private void GetTrainerPanel()
        {
            uint PetId = Request.PopWiredUInt();
            Pet PetData = null;

            Room Room = Session.GetHabbo().CurrentRoom;

            if (Room == null)
            {
                return;
            }

            if ((PetData = Room.GetPet(PetId).PetData) == null)
            {
                return;
            }
            else
            {
                int Level = PetData.Level;
                PetData = null;

                GetResponse().Init(605);
                GetResponse().AppendUInt(PetId);
                GetResponse().AppendInt32(18);

                GetResponse().AppendBoolean(false);

                for (int i = 0; i < 18; i++)
                {
                    GetResponse().AppendInt32(i + 1);
                }

                GetResponse().AppendBoolean(false);

                for (int i = 0; i < Level; i++)
                {
                    GetResponse().AppendInt32(i);
                }

                SendResponse();
            }
        }

        public void RegisterRooms()
        {
            RequestHandlers[391] = new RequestHandler(OpenFlat);
            RequestHandlers[182] = new RequestHandler(GetAdvertisement);
            RequestHandlers[388] = new RequestHandler(GetPub);
            RequestHandlers[2] = new RequestHandler(OpenPub);
            RequestHandlers[230] = new RequestHandler(GetGroupBadges);
            RequestHandlers[215] = new RequestHandler(GetRoomData1);
            RequestHandlers[390] = new RequestHandler(GetRoomData2);
            RequestHandlers[126] = new RequestHandler(GetRoomData3);
            RequestHandlers[52] = new RequestHandler(Talk);
            RequestHandlers[55] = new RequestHandler(Shout);
            RequestHandlers[56] = new RequestHandler(Whisper);
            RequestHandlers[75] = new RequestHandler(Move);
            RequestHandlers[387] = new RequestHandler(CanCreateRoom);
            RequestHandlers[29] = new RequestHandler(CreateRoom);
            RequestHandlers[400] = new RequestHandler(GetRoomEditData);
            RequestHandlers[386] = new RequestHandler(SaveRoomIcon);
            RequestHandlers[401] = new RequestHandler(SaveRoomData);
            RequestHandlers[96] = new RequestHandler(GiveRights);
            RequestHandlers[97] = new RequestHandler(TakeRights);
            RequestHandlers[155] = new RequestHandler(TakeAllRights);
            RequestHandlers[95] = new RequestHandler(KickUser);
            RequestHandlers[320] = new RequestHandler(BanUser);
            RequestHandlers[71] = new RequestHandler(InitTrade);
            RequestHandlers[384] = new RequestHandler(SetHomeRoom);
            RequestHandlers[23] = new RequestHandler(DeleteRoom);
            RequestHandlers[79] = new RequestHandler(LookAt);
            RequestHandlers[317] = new RequestHandler(StartTyping);
            RequestHandlers[318] = new RequestHandler(StopTyping);
            RequestHandlers[319] = new RequestHandler(IgnoreUser);
            RequestHandlers[322] = new RequestHandler(UnignoreUser);
            RequestHandlers[345] = new RequestHandler(CanCreateRoomEvent);
            RequestHandlers[346] = new RequestHandler(StartEvent);
            RequestHandlers[347] = new RequestHandler(StopEvent);
            RequestHandlers[348] = new RequestHandler(EditEvent);
            RequestHandlers[94] = new RequestHandler(Wave);
            RequestHandlers[263] = new RequestHandler(GetUserTags);
            RequestHandlers[159] = new RequestHandler(GetUserBadges);
            RequestHandlers[261] = new RequestHandler(RateRoom);
            RequestHandlers[93] = new RequestHandler(Dance);
            RequestHandlers[98] = new RequestHandler(AnswerDoorbell);
            RequestHandlers[59] = new RequestHandler(ReqLoadRoomForUser);
            RequestHandlers[66] = new RequestHandler(ApplyRoomEffect);
            RequestHandlers[90] = new RequestHandler(PlaceItem);
            RequestHandlers[67] = new RequestHandler(TakeItem);
            RequestHandlers[73] = new RequestHandler(MoveItem);
            RequestHandlers[392] = new RequestHandler(TriggerItem); // Generic trigger item
            RequestHandlers[393] = new RequestHandler(TriggerItem); // Generic trigger item
            RequestHandlers[83] = new RequestHandler(OpenPostit);
            RequestHandlers[84] = new RequestHandler(SavePostit);
            RequestHandlers[85] = new RequestHandler(DeletePostit);
            RequestHandlers[78] = new RequestHandler(OpenPresent);
            RequestHandlers[341] = new RequestHandler(GetMoodlight);
            RequestHandlers[342] = new RequestHandler(UpdateMoodlight);
            RequestHandlers[343] = new RequestHandler(SwitchMoodlightStatus);
            RequestHandlers[72] = new RequestHandler(OfferTradeItem);
            RequestHandlers[405] = new RequestHandler(TakeBackTradeItem);
            RequestHandlers[70] = new RequestHandler(StopTrade);
            RequestHandlers[403] = new RequestHandler(StopTrade);
            RequestHandlers[69] = new RequestHandler(AcceptTrade);
            RequestHandlers[68] = new RequestHandler(UnacceptTrade);
            RequestHandlers[402] = new RequestHandler(CompleteTrade);
            RequestHandlers[371] = new RequestHandler(GiveRespect);
            RequestHandlers[372] = new RequestHandler(ApplyEffect);
            RequestHandlers[373] = new RequestHandler(EnableEffect);
            RequestHandlers[232] = new RequestHandler(TriggerItem); // One way gates
            RequestHandlers[314] = new RequestHandler(TriggerItem); // Love Shuffler
            RequestHandlers[247] = new RequestHandler(TriggerItem); // Habbo Wheel
            RequestHandlers[76] = new RequestHandler(TriggerItem); // Dice
            RequestHandlers[77] = new RequestHandler(TriggerItemDiceSpecial); // Dice (special)
            RequestHandlers[414] = new RequestHandler(RecycleItems);
            RequestHandlers[183] = new RequestHandler(RedeemExchangeFurni);
            RequestHandlers[113] = new RequestHandler(EnterInfobus);
            RequestHandlers[258] = new RequestHandler(GetJukeboxSongs);
            RequestHandlers[441] = new RequestHandler(KickBot);
            RequestHandlers[3002] = new RequestHandler(PlacePet);
            RequestHandlers[3001] = new RequestHandler(GetPetInfo);
            RequestHandlers[3003] = new RequestHandler(PickUpPet);
            RequestHandlers[3005] = new RequestHandler(RespectPet);
            RequestHandlers[3004] = new RequestHandler(GetTrainerPanel);
            RequestHandlers[3254] = new RequestHandler(PlaceSticky);
        }
    }
}